<?php

namespace addons\qingdongams\controller;

use addons\qingdongams\model\Message;
use addons\qingdongams\model\ReceivablesPlan as ReceivablesPlanModel;
use addons\qingdongams\model\Staff;
use think\Db;
use think\Exception;

/**
 * 回款计划
 */
class ReceivablesPlan extends StaffApi {
    protected $noNeedLogin = [];
    protected $noNeedRight = [];


    //新增回款计划
    public function addPlan() {
        $params = $this->request->post();

        // 表单验证
        if (($result = $this->qingdongamsValidate($params, get_class(), 'create')) !== true) {
            $this->error($result);
        }
        if (ReceivablesPlanModel::where([
            'num'         => $params['num'],
            'contract_id' => $params['contract_id']
        ])->find()) {
            $this->error('计划回款期数已存在');
        }

        Db::startTrans();
        try {
            ReceivablesPlanModel::createPlan($params);

            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success('新增回款计划成功');
        }
    }


    //获取回款计划列表
    public function getList()
    {
        $customer_id = input('customer_id');
        $contract_id = input('contract_id');
        $times = input('times');
        $staff_id = input('staff_id');
        $status = input('status', null);
        $limit = input('limit');
        $type = input('type',0);
        $where = [];
        if ($customer_id) {
            $where['customer_id'] = $customer_id;
        }
        if ($contract_id) {
            $where['contract_id'] = $contract_id;
        }
        if ($staff_id) {
            $where['owner_staff_id'] = $staff_id;
        } else {

            if ($type == 1) {//我的客户
                $where['owner_staff_id'] = $this->auth->id;
            } elseif ($type == 2) {//下属负责的客户
                $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
            }else{
                $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
            }

        }
        if ($times) {//计划回款日期
            $times = explode(',', $times);
            $where['return_date'] = ['between', [$times[0], $times[1]]];
        }
        if($status !== null){
            $where['status'] = $status;
        }

        $list = ReceivablesPlanModel::where($where)->with(['contract', 'customer', 'createStaff'])
            ->order('id desc')->paginate($limit);
        $this->success('请求成功', $list);
    }


    //获取select回款计划列表
    public function getSelectList() {
        $contract_id          = input('contract_id');
        $where                = [];
        $where['contract_id'] = $contract_id;
        $where['status']      = 0;
        $list                 = ReceivablesPlanModel::where($where)->select();
        $this->success('请求成功', $list);
    }


    //回款详情
    public function getDetail() {
        $id       = input('id');
        $customer = ReceivablesPlanModel::where(['id' => $id])->with(['contract', 'customer', 'createStaff'])->find();
        //标记通知已读
        Message::setRead(Message::PLAN_TYPE, $id, $this->auth->id);
        $this->success('请求成功', $customer);
    }
}
